<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="付款人标识" prop="userTagId">
        <el-input
          v-model="queryParams.userTagId"
          placeholder="请输入付款人标识"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="渠道商号" prop="huifuId">
        <el-input
          v-model="queryParams.huifuId"
          placeholder="请输入渠道商号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="商户号" prop="sysId">
        <el-input
          v-model="queryParams.sysId"
          placeholder="请输入商户号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="商户简称" prop="sysShortName">
        <el-input
          v-model="queryParams.sysShortName"
          placeholder="请输入商户简称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="商户名称" prop="sysIdName">
        <el-input
          v-model="queryParams.sysIdName"
          placeholder="请输入商户名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="产品号" prop="productNumber">
        <el-input
          v-model="queryParams.productNumber"
          placeholder="请输入产品号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="汇付终端id" prop="huiTerminalId">
        <el-input
          v-model="queryParams.huiTerminalId"
          placeholder="请输入汇付终端id"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="终端名称" prop="huiTerminalName">
        <el-input
          v-model="queryParams.huiTerminalName"
          placeholder="请输入终端名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="终端类型" prop="huiTerminalType">
        <el-select
          v-model="queryParams.huiTerminalType"
          placeholder="请选择终端类型"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="凭证号" prop="pzNumber">
        <el-input
          v-model="queryParams.pzNumber"
          placeholder="请输入凭证号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="汇付订单号" prop="huifuOrderId">
        <el-input
          v-model="queryParams.huifuOrderId"
          placeholder="请输入汇付订单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="外部订单号" prop="outsideOrderId">
        <el-input
          v-model="queryParams.outsideOrderId"
          placeholder="请输入外部订单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="交易类型" prop="exchangeType">
        <el-select
          v-model="queryParams.exchangeType"
          placeholder="请选择交易类型"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="交易方式订单号" prop="exchangeWaySeq">
        <el-input
          v-model="queryParams.exchangeWaySeq"
          placeholder="请输入交易方式订单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="交易方式 微信支付宝等" prop="exchangeWay">
        <el-input
          v-model="queryParams.exchangeWay"
          placeholder="请输入交易方式 微信支付宝等"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="商品描述" prop="productRemarks">
        <el-input
          v-model="queryParams.productRemarks"
          placeholder="请输入商品描述"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="交易金额" prop="exchangeMoney">
        <el-input
          v-model="queryParams.exchangeMoney"
          placeholder="请输入交易金额"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="手续费" prop="handlingFee">
        <el-input
          v-model="queryParams.handlingFee"
          placeholder="请输入手续费"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="退款金额" prop="refundAmount">
        <el-input
          v-model="queryParams.refundAmount"
          placeholder="请输入退款金额"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="交易状态" prop="payStatus">
        <el-select
          v-model="queryParams.payStatus"
          placeholder="请选择交易状态"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="交易时间" prop="exchangeTime">
        <el-date-picker
          v-model="queryParams.exchangeTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="付款银行" prop="payBank">
        <el-input
          v-model="queryParams.payBank"
          placeholder="请输入付款银行"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="借贷标识" prop="loanTag">
        <el-input
          v-model="queryParams.loanTag"
          placeholder="请输入借贷标识"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['huifu:order:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['huifu:order:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="付款人标识" align="center" prop="userTagId" />
      <el-table-column label="渠道商号" align="center" prop="huifuId" />
      <el-table-column label="商户号" align="center" prop="sysId" />
      <el-table-column label="商户简称" align="center" prop="sysShortName" />
      <el-table-column label="商户名称" align="center" prop="sysIdName" />
      <el-table-column label="产品号" align="center" prop="productNumber" />
      <el-table-column label="汇付终端id" align="center" prop="huiTerminalId" />
      <el-table-column label="终端名称" align="center" prop="huiTerminalName" />
      <el-table-column label="终端类型" align="center" prop="huiTerminalType" />
      <el-table-column label="凭证号" align="center" prop="pzNumber" />
      <el-table-column label="汇付订单号" align="center" prop="huifuOrderId" />
      <el-table-column label="外部订单号" align="center" prop="outsideOrderId" />
      <el-table-column label="交易类型" align="center" prop="exchangeType" />
      <el-table-column label="交易方式订单号" align="center" prop="exchangeWaySeq" />
      <el-table-column label="交易方式 微信支付宝等" align="center" prop="exchangeWay" />
      <el-table-column label="商品描述" align="center" prop="productRemarks" />
      <el-table-column label="交易金额" align="center" prop="exchangeMoney" />
      <el-table-column label="手续费" align="center" prop="handlingFee" />
      <el-table-column label="退款金额" align="center" prop="refundAmount" />
      <el-table-column label="交易状态" align="center" prop="payStatus" />
      <el-table-column
        label="交易时间"
        align="center"
        prop="exchangeTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="付款银行" align="center" prop="payBank" />
      <el-table-column label="借贷标识" align="center" prop="loanTag" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['huifu:order:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['huifu:order:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <OrderForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { OrderApi, OrderVO } from '@/api/huifu/order'
import OrderForm from './OrderForm.vue'

/** 汇付对帐订单 列表 */
defineOptions({ name: 'HuifuOrder' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<OrderVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  userTagId: undefined,
  huifuId: undefined,
  sysId: undefined,
  sysShortName: undefined,
  sysIdName: undefined,
  productNumber: undefined,
  huiTerminalId: undefined,
  huiTerminalName: undefined,
  huiTerminalType: undefined,
  pzNumber: undefined,
  huifuOrderId: undefined,
  outsideOrderId: undefined,
  exchangeType: undefined,
  exchangeWaySeq: undefined,
  exchangeWay: undefined,
  productRemarks: undefined,
  exchangeMoney: undefined,
  handlingFee: undefined,
  refundAmount: undefined,
  payStatus: undefined,
  exchangeTime: [],
  payBank: undefined,
  loanTag: undefined,
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await OrderApi.getOrderPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await OrderApi.deleteOrder(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await OrderApi.exportOrder(queryParams)
    download.excel(data, '汇付对帐订单.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>